package com.outdd.vaietool.common.aop;

import com.outdd.vaietool.common.utils.AopUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * api日志解析拦截器
 * @author vaie
 */
@Aspect
@Component
@Slf4j
@Order(0)
public class AopAspect {


    static final String  EDP = "@annotation(com.outdd.vaietool.common.annotation.AopLog)";


    /**
     * 环绕通知
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around(EDP)
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
        log.debug("AopAspect");

        log.info("{}"+System.lineSeparator()+"{}服务调用开始,参数：{}"
                , AopUtil.urlMethod()
                , AopUtil.getAnnotationValue(pjp)
                , AopUtil.getArgsString(pjp));

        long statTime = System.currentTimeMillis();


        //执行方法
        Object result=pjp.proceed();

        long resTime = System.currentTimeMillis()-statTime;


        log.info("{}服务结束,共耗时:{} (millis),返回值：{}",
                AopUtil.getAnnotationValue(pjp),
                resTime,
                AopUtil.getResStr(result)
                );


        //过滤不需要的字段
        return AopUtil.filtrationField(result);
    }

}
